package cn.com.cetccst.distribution;

import static cn.com.cetccst.tools.kv.CommonKV.kvStore;

import android.content.Context;
import android.text.TextUtils;

import cn.com.cetccst.datacenter.policy.PolicyRepository;
import cn.com.cetccst.distribution.action.CheckIn;
import cn.com.cetccst.distribution.action.InboxMessageAction;
import cn.com.cetccst.distribution.app.AppInfoManager;
import cn.com.cetccst.distribution.kv.KVKeys;
import cn.com.cetccst.distribution.mqtt.DistributionMqtt;
import cn.com.cetccst.distribution.network.DistributionHttpRequester;
import cn.com.cetccst.mqtt.MqttManager;
import cn.com.cetccst.tools.log.CommonLogger;

public class DistributionManager {
    private static DistributionManager instance;

    private DistributionManager(Context context, String mqttUrl) {
        MqttManager.setLogger(CommonLogger.getLogger());
        DistributionMqtt.initialize(context, mqttUrl);
        PolicyRepository.initialize(context);
        AppInfoManager.initialize(context);
        if (!TextUtils.isEmpty(kvStore.decodeString(KVKeys.KEY_BASE_URL))) {
            DistributionHttpRequester.initialize(context, kvStore.decodeString(KVKeys.KEY_BASE_URL));
        }
        subscribeBusiness(context);
    }

    /**
     * action动作订阅业务business(topic)
     */
    private void subscribeBusiness(Context context) {
        CheckIn.subscribeBusiness(context);
        InboxMessageAction.subscribeBusiness(context);
    }

    /**
     * TODO 初始化参数等后续协议定了再调整
     *
     * @param context
     * @param mqttUrl
     */
    public static void initialize(Context context, String mqttUrl) {

        if (instance == null)
            instance = new DistributionManager(context, mqttUrl);
    }

    public static DistributionManager get() {
        return instance;
    }

}
